package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Administrator on 2016.8.2.
 */
public class Test1603 extends JFrame{
    Paint paint;
    Menue menue;

    public Test1603(){
        paint=new Paint();
        menue=new Menue();
        this.add(paint,BorderLayout.CENTER);
        this.add(menue,BorderLayout.SOUTH);
    }

    public static void main(String[] args){
        Test1603 frame=new Test1603();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Welcome to JAVA");
        frame.setLocationRelativeTo(null);
        frame.setSize(300,300);
        frame.setVisible(true);
    }

    private class Paint extends JPanel{
        final int radius=5;
        int x;
        int y;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawOval(x,y,radius,radius);
        }

        private Paint() {
            x = getWidth()/2;
            y = getHeight()/2;
            repaint();
        }

        public void setX(int x){
            this.x=x;
            repaint();
        }

        public void setY(int y){
            this.y=y;
            repaint();
        }

        public int getX(){
            return x;
        }

        public int getY(){
            return y;
        }
    }

    private class Menue extends JPanel{
        JButton Ljbt;
        JButton Rjbt;
        JButton Ujbt;
        JButton Djbt;

        public Menue(){
            Ljbt=new JButton("Left");
            Rjbt=new JButton("Right");
            Ujbt=new JButton("Up");
            Djbt=new JButton("Down");

            Ljbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1603.this.paint.setX(Test1603.this.paint.getX()-5);
                }
            });

            Rjbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1603.this.paint.setX(Test1603.this.paint.getX()+5);
                }
            });

            Ujbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1603.this.paint.setY(Test1603.this.paint.getY()-5);
                }
            });

            Djbt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Test1603.this.paint.setY(Test1603.this.paint.getY()+5);
                }
            });

            this.setLayout(new FlowLayout());
            this.add(Ljbt);
            this.add(Rjbt);
            this.add(Ujbt);
            this.add(Djbt);
        }
    }
}
